home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / editorApplicationOverlay.js < prev    next >
Encoding:
JavaScript  |  2002-04-12  |  6.4 KB  |  202 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998-1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the NPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the NPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /* Implementations of nsIControllerCommand for composer commands */
  39.  
  40. function initEditorContextMenuItems(aEvent)
  41. {
  42.   var shouldShowEditPage = !gContextMenu.onImage && !gContextMenu.onLink && !gContextMenu.onTextInput && !gContextMenu.inDirList;
  43.   gContextMenu.showItem( "context-editpage", shouldShowEditPage );
  44.  
  45.   var shouldShowEditLink = gContextMenu.onSaveableLink; 
  46.   gContextMenu.showItem( "context-editlink", shouldShowEditLink );
  47.  
  48.   // Hide the applications separator if there's no add-on apps present. 
  49.   gContextMenu.showItem("context-sep-apps", gContextMenu.shouldShowSeparator("context-sep-apps"));
  50. }
  51.   
  52. function initEditorContextMenuListener(aEvent)
  53. {
  54.   var popup = document.getElementById("contentAreaContextMenu");
  55.   if (popup)
  56.     popup.addEventListener("popupshowing", initEditorContextMenuItems, false);
  57. }
  58.  
  59. addEventListener("load", initEditorContextMenuListener, false);
  60.  
  61. function editLink(aLinkURL)
  62. {
  63.   urlSecurityCheck(aLinkURL, window.document);  // XXX what is this? Why do we pass the chrome doc?
  64.   editPage(aLinkURL, window, false);
  65. }
  66.  
  67. function editDocument(aDocument)      
  68. {
  69.   if (!aDocument)
  70.     aDocument = window._content.document;
  71.  
  72.   editPage(aDocument.URL, window, false); 
  73. }
  74.  
  75. function editPageOrFrame()
  76. {
  77.   var url;
  78.   var focusedWindow = document.commandDispatcher.focusedWindow;
  79.   if (isDocumentFrame(focusedWindow))
  80.     url = focusedWindow.location.href;
  81.   else
  82.     url = window._content.location.href;
  83.  
  84.   editPage(url, window, false)
  85. }
  86.  
  87. // Any non-editor window wanting to create an editor with a URL
  88. //   should use this instead of "window.openDialog..."
  89. //  We must always find an existing window with requested URL
  90. // (When calling from a dialog, "launchWindow" is dialog's "opener"
  91. //   and we need a delay to let dialog close)
  92. function editPage(url, launchWindow, delay)
  93. {
  94.   var focusedWindow = document.commandDispatcher.focusedWindow;
  95.   if (isDocumentFrame(focusedWindow))
  96.     url = focusedWindow.location.href;
  97.  
  98.   // User may not have supplied a window
  99.   if (!launchWindow)
  100.   {
  101.     if (window)
  102.     {
  103.       launchWindow = window;
  104.     }
  105.     else
  106.     {
  107.       dump("No window to launch an editor from!\n");
  108.       return;
  109.     }
  110.   }
  111.  
  112.   // if the current window is a browser window, then extract the current charset menu setting from the current 
  113.   // document and use it to initialize the new composer window...
  114.  
  115.   var wintype = document.firstChild.getAttribute('windowtype');
  116.   var charsetArg;
  117.  
  118.   if (launchWindow && (wintype == "navigator:browser") && launchWindow._content.document)
  119.     charsetArg = "charset=" + launchWindow._content.document.characterSet;
  120.  
  121.   try {
  122.     var uri = createURI(url, null, null);
  123.  
  124.     var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();
  125.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  126.     var enumerator = windowManagerInterface.getEnumerator( "composer:html" );
  127.     var emptyWindow;
  128.     while ( enumerator.hasMoreElements() )
  129.     {
  130.       var win = windowManagerInterface.convertISupportsToDOMWindow( enumerator.getNext() );
  131.       if ( win && win.editorShell)
  132.       {
  133.         if (CheckOpenWindowForURIMatch(uri, win))
  134.         {
  135.           // We found an editor with our url
  136.           win.focus();
  137.           return;
  138.         }
  139.         else if (!emptyWindow && win.PageIsEmptyAndUntouched())
  140.         {
  141.           emptyWindow = win;
  142.         }
  143.       }
  144.     }
  145.  
  146.     if (emptyWindow)
  147.     {
  148.       // we have an empty window we can use
  149.       if (emptyWindow.IsInHTMLSourceMode())
  150.         emptyWindow.FinishHTMLSource();
  151.       emptyWindow.editorShell.LoadUrl(url);
  152.       emptyWindow.focus();
  153.       emptyWindow.SetSaveAndPublishUI(url);
  154.       return;
  155.     }
  156.  
  157.     // Create new Composer window
  158.     if (delay)
  159.     {
  160.       dump("delaying\n");
  161.       launchWindow.delayedOpenWindow("chrome://editor/content", "chrome,all,dialog=no", url);
  162.     }
  163.     else
  164.       launchWindow.openDialog("chrome://editor/content", "_blank", "chrome,all,dialog=no", url, charsetArg);
  165.  
  166.   } catch(e) {}
  167. }
  168.  
  169. function createURI(urlstring)
  170. {
  171.   try {
  172.     var ioserv = Components.classes["@mozilla.org/network/io-service;1"]
  173.                .getService(Components.interfaces.nsIIOService);
  174.     return ioserv.newURI(urlstring, null, null);
  175.   } catch (e) {}
  176.  
  177.   return null;
  178. }
  179.  
  180. function CheckOpenWindowForURIMatch(uri, win)
  181. {
  182.   try {
  183.     var contentWindow = win.content;  // need to QI win to nsIDOMWindowInternal?
  184.     var contentDoc = contentWindow.document;
  185.     var htmlDoc = contentDoc.QueryInterface(Components.interfaces.nsIDOMHTMLDocument);
  186.     var winuri = createURI(htmlDoc.URL);
  187.     return winuri.equals(uri);
  188.   } catch (e) {}
  189.   
  190.   return false;
  191. }
  192.  
  193. function NewEditorFromTemplate()
  194. {
  195.   // XXX not implemented
  196. }
  197.  
  198. function NewEditorFromDraft()
  199. {
  200.   // XXX not implemented
  201. }
  202.